package com.example.oms.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.example.oms.dto.entity.OmsSmsTaskEntity;
import com.example.oms.dto.ro.SmsCallBackRO;
import com.example.oms.mapper.OmsSmsTaskMapper;
import com.example.oms.service.SmsService;
import com.example.oms.service.SmsStepService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


@Service
public class SmsServiceImpl implements SmsService {

    @Resource
    private OmsSmsTaskMapper omsSmsTaskMapper;

    final static String DELIVRD = "DELIVRD";
    private final Map<String, SmsStepService> stepMap;

    public SmsServiceImpl() {
        stepMap = new HashMap<>();
        stepMap.put("LOGIN", new LoginStepServiceImpl());
        stepMap.put("DELIVERY", new DeliveryStepServiceImpl());
        stepMap.put("FINISH", new FinishStepServiceImpl());
    }


    /**
     * 根据taskId 去获取订单id,和短信流程,再去做订单修改
     *
     * @param ro 回调参数
     * @return 返回状态码
     */
    @Transactional
    @Override
    public String SMSCallBack(SmsCallBackRO ro) {
        if (ObjectUtil.isNotEmpty(ro) && ObjectUtil.isNotEmpty(ro.getStatus())) {
            if (ObjectUtil.equals(ro.getStatus(), DELIVRD)) {
                OmsSmsTaskEntity omsSmsTaskEntity = omsSmsTaskMapper.selectById(ro.getSendId());
                SmsStepService smsStepService = stepMap.get(omsSmsTaskEntity.getSmsStep());
                smsStepService.handleSmsStatus(ro);
            } else {
                System.out.println("Status is not DELIVRD");
            }
        }
        return "ok";
    }

}